package com.itzyf.pokemondata.activity.setting

import androidx.recyclerview.widget.LinearLayoutManager
import com.avos.avoscloud.AVException
import com.avos.avoscloud.AVObject
import com.avos.avoscloud.AVQuery
import com.avos.avoscloud.FindCallback
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_main.*

/**
 * @author 依风听雨
 * @version 创建时间：2019/03/01 09:00
 */
class FeedbackListActivity : BaseActivity() {
    private lateinit var mAdapter: QuickAdapter
    var mDatas: MutableList<AVObject?> = ArrayList()

    override fun handlerActivity() {
        mAdapter = QuickAdapter(R.layout.item_feedback, mDatas)

        rv_list.layoutManager = LinearLayoutManager(this)
        rv_list.adapter = mAdapter

        val feedback = AVQuery<AVObject>("feedback")
        feedback.orderByDescending("createdAt")
        feedback.limit = 20
        showProgressDialog()
        feedback.findInBackground(object : FindCallback<AVObject>() {
            override fun done(avObjects: MutableList<AVObject>?, e: AVException?) {
                dismissProgressDialog()
                e?.printStackTrace()
                if (avObjects == null)
                    return
                mDatas.addAll(avObjects)
                mAdapter.notifyDataSetChanged()
            }
        })

    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<AVObject?>) :
        BaseQuickAdapter<AVObject, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: AVObject?) {
            item?.let {
                helper.setText(R.id.tvFeedback, it.getString("content"))
                helper.setGone(R.id.tvReply, !it.getString("reply").isNullOrEmpty())
                helper.setText(R.id.tvReply, it.getString("reply"))
            }

        }
    }

    override fun getContentViewId() = R.layout.activity_main

    override fun getToolbarTitle() = "反馈列表"

}